package com.qxzn.app.personalcenter.activity;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.personalcenter.R;
import com.qxzn.app.personalcenter.R2;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.CommonUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 注册3-密码
 */
public class RegisterPwdActivity extends BaseActivity {
    @BindView(R2.id.et_pwd)
    EditText etPwd;
    @BindView(R2.id.et_security_pwd)
    EditText etSecurityPwd;
    private String pwd;
    private String securityPwd;

    @Override
    protected int initLayout() {
        return R.layout.activity_register_pwd;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void iniTitleBar() {
        tv_title.setVisibility(View.GONE);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        etPwd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16), new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Pattern p = Pattern.compile("([\\ \\\\\\[\\]!\"#$%&'()*+,-./:;<=>?@^_`{|}~]|[0-9a-zA-Z])+");
                Matcher m = p.matcher(source.toString());
                if (!m.matches()) return "";
                return null;
            }
        }});
    }

    @OnClick(R2.id.tv_next)
    public void onViewClicked() {
        if (CommonUtil.isFastClick()) {
            return;
        }
        pwd = etPwd.getText().toString().trim();
        securityPwd = etSecurityPwd.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            AppUtil.showToast(this, etPwd.getHint().toString());
            return;
        }
        if (!CommonUtil.isLettersAndNumbers(pwd)) {
            AppUtil.showToast(this, "密码不能少于8个字符，至少包含数字和字母");
            return;
        }
        if (TextUtils.isEmpty(securityPwd)) {
            AppUtil.showToast(this, etSecurityPwd.getHint().toString());
            return;
        }
        if (securityPwd.length() == 0) {
            AppUtil.showToast(this, etSecurityPwd.getHint().toString());
            return;
        } else if (securityPwd.length() != 6) {
            AppUtil.showToast(this, "安全码应为6位数字");
            return;
        }
        startActivity(getIntent().setClass(this, RegisterNicknameActivity.class)
                .putExtra("pwd", etPwd.getText().toString().trim())
                .putExtra("security_pwd", etSecurityPwd.getText().toString().trim()));
    }
}
